// 引入操作数据库的方法
const db = require('../db/index')
    // var path = require("path");
var _ = require('lodash');
const { query } = require('express');
const { result } = require('lodash');
// const { array } = require('joi');
// const { add, split } = require('lodash');
// 获取列表
exports.goods = (req, res) => {
        // console.log(req.query);
        querys = req.query.query
        pagenum = req.query.pagenum
        pagesize = req.query.pagesize
            // console.log(querys);
        if (querys) {
            querys = "'" + "%" + querys + "%" + "'"
        } else {
            querys = "'" + "%" + "%" + "'"
        }
        // console.log(querys);
        sql = "SELECT * FROM  sp_goods WHERE goods_name LIKE " + querys;
        db.query(sql, (err, status) => {
            if (err) return res.ResData(err)
                // console.log(status);
            if (pagenum && pagesize) {
                count = status.length //总条数 
                pagenum = parseInt(pagenum)
                    // console.log(pagenum);
                pagesize = parseInt(pagesize)
                    // console.log(pagesize);
                    // if (querys = "'" + "%" + querys + "%" + "'" && pagenum !== 1) {
                    //     pagenum = 1
                    // }
                data = _.take(_.drop(status, (pagenum - 1) * pagesize), pagesize)
                    // console.log(data);
                data = {
                    "total": count,
                    "pagenum": pagenum,
                    "pagesize": pagesize,
                    data
                }
                return res.ResData("获取成功", 200, { "total": count, "pagenum": pagenum, data })
            }
            res.ResData("获取成功", 200, { "total": count, "pagenum": pagenum, data })
        })
    }
    //根据id删除
exports.deletegoods = (req, res) => {
        // console.log(req.params);
        id = req.params.id
        sql = 'DELETE  FROM sp_goods WHERE goods_id=?'
        db.query(sql, [id], (err, result) => {
            if (err) return res.ResData('删除失败！')
            res.ResData('删除成功！', 200, null)
        })
    }
    // 添加商品
    // 有问题
exports.selectgoods = (req, res) => {
    // console.log(req.body);
    // goods_name = req.body.goods_name
    // goods_price = req.body.goods_price
    // goods_number = req.body.goods_number
    // goods_weight = req.body.goods_weight
    // goods_introduce = req.body.goods_introduce
    //     // console.log(goods_name, goods_price, goods_number, goods_weight, goods_introduce);
    // sql = "INSERT INTO `sp_goods` VALUES (DEFAULT, ?, ?, ?, ?, DEFAULT, ?, DEFAULT, DEFAULT, ?, DEFAULT,DEFAULT,DEFAULT, DEFAULT,DEFAULT,DEFAULT,DEFAULT,DEFAULT,DEFAULT);"
    // db.query(sql, [goods_name, goods_price, goods_number, goods_weight, goods_introduce, 0], (err, status) => {
    //     console.log(err);
    //     if (err) return res.ResData(err)
    //     console.log(status);
    //     sql1 = "SELECT*FROM sp_goods WHERE goods_name=? AND goods_price=? AND goods_number=? AND goods_weight=? AND goods_introduce=?"
    //     db.query(sql1, [goods_name, goods_price, goods_number, goods_weight, goods_introduce], (err, status) => {
    //         if (err) return res.ResData(err)
    //         console.log(status);
    //         res.ResData("创建商品成功", 201, {
    //             "goods_id": status[0].goods_id,
    //             "goods_name": status[0].goods.attr_name,
    //             "goods_price": status[0].goods_price,
    //             "cat_id": status[0].cat_id,
    //             "goods_number": status[0].goods_number,
    //             "goods_weight": status[0].goods_weight,
    //             "goods_introduce": status[0].goods_introduce,
    //             "goods_big_logo": status[0].goods_big_logo,
    //             "goods_small_logo": status[0].goods_small_logo,
    //             "goods_state": status[0].goods_state,
    //             "add_time": status[0].add_time,
    //             "upd_time": status[0].upd_time,
    //             "hot_mumber": status[0].hot_mumber,
    //             "is_promote": status[0].is_promote
    //         })
    //     })
    // })
}